﻿using FastGPT_Admin.Service;
using FastGPT_Admin.Setting;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace FastGPT_Admin.Controller
{
    internal class UsersController
    {
        private string connectionString = AppSettings.GlobalSettingsInstance.ConnectionString;
        private string databaseName = "fastgpt";
        private string collectionName = "users";
        private UsersService _usersService;

        public UsersController()
        {
            _usersService = new UsersService(connectionString, databaseName, collectionName);
        }

        public string CheckUsersUsername(string Username)
        {
            List<Users> users = _usersService.Read(Username);
            if (users == null || users.Count != 1)
            {
                return "Null";
            }
            else
            {
                return users[0].Id.ToString();
            }
        }

        public ObjectId CreateUsers(Users model)
        {
            try
            {
                ObjectId userId = _usersService.Create(model);
                return userId;
            } catch (Exception ex)
            {
                return ObjectId.Empty;
            }
        }

        public bool UpdateUsers(string Id, Users model)
        {
            try
            {
                bool status = _usersService.Update(Id, model);
                return status;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
